package com.ttg.web.task;

import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.JsonUtils;
import com.ttg.common.dto.TaskBaseDTO;
import com.ttg.common.vo.TaskResultVO;
import com.ttg.web.service.job.*;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * Description：质量管理相关任务
 * Author: chenyou
 * Date: 2025/8/27 17:36
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Component
@EnableScheduling
@Slf4j
public class QualityManageJobTask {

    @Autowired
    private QualityManageJobService qualityManageJobService;



//    @Scheduled(cron = "0/30 * * * * ? ")
    @XxlJob("industryScore")
    @Async
    public ReturnT<String> industryScore() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask industryScore 行业得分计算任务】 start=>{}",param);
        try {
            TaskBaseDTO reqDto = new TaskBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskBaseDTO.class);
            }
            TaskResultVO resultVO = qualityManageJobService.industryScore(reqDto);
            System.out.println(resultVO.toString());
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask industryScore 行业得分计算任务】 end");
        return ReturnT.SUCCESS;
    }


//    @Scheduled(cron = "0/30 * * * * ? ")
    @XxlJob("industryClassification")
    @Async
    public ReturnT<String> industryClassification() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask industryClassification 行业质量类别计算任务】 start=>{}",param);
        try {
            TaskBaseDTO reqDto = new TaskBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskBaseDTO.class);
            }
            TaskResultVO resultVO = qualityManageJobService.industryClassification(reqDto);
            System.out.println(resultVO.toString());
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask industryClassification 行业质量类别计算任务】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("merchScore")
    @Async
    public ReturnT<String> merchScore() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask merchScore 商户得分计算任务】 start=>{}",param);
        try {
            TaskBaseDTO reqDto = new TaskBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskBaseDTO.class);
            }
            TaskResultVO resultVO = qualityManageJobService.merchScore(reqDto);
            System.out.println(resultVO.toString());
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask industryScore 商户得分计算任务】 end");
        return ReturnT.SUCCESS;
    }


    //    @Scheduled(cron = "0/30 * * * * ? ")
    @XxlJob("merchClassification")
    @Async
    public ReturnT<String> merchClassification() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask merchClassification 商户质量类别计算任务】 start=>{}",param);
        try {
            TaskBaseDTO reqDto = new TaskBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskBaseDTO.class);
            }
            TaskResultVO resultVO = qualityManageJobService.merchClassification(reqDto);
            System.out.println(resultVO.toString());
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask merchClassification 商户质量类别计算任务】 end");
        return ReturnT.SUCCESS;
    }
}
